package com.tms.app.tms.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.sdk.android.ams.common.util.StringUtil;
import com.tms.app.tms.R;
import com.tms.app.tms.bean.GoodsInfo;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.GoodsRes;
import com.tms.app.tms.ui.base.ui.BaseListActivity;
import com.tms.app.tms.utils.SharePreferenceUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/31 0031.
 */

public class AddGoodsActivity extends BaseListActivity {
    GoodsPresenter presenter;
    @BindView(R.id.edit_1)
    EditText edit1;
    @BindView(R.id.edit_2)
    EditText edit2;
    @BindView(R.id.edit_3)
    EditText edit3;
    @BindView(R.id.edit_4)
    EditText edit4;
    @BindView(R.id.edit_5)
    EditText edit5;
    @BindView(R.id.button_send)
    Button buttonSend;
    private GoodsInfo goodInfo;
    private int dispatchId=-1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_goods_add);
        ButterKnife.bind(this);
        String feeJson = getIntent().getStringExtra("goodInfo");
        dispatchId = getIntent().getIntExtra("dispatchId", 0);
        token= SharePreferenceUtil.getToken(this);
        presenter = new GoodsPresenter();
        presenter.setiView(this);
        if (feeJson != null && feeJson.length() > 1) {
            goodInfo = gson.fromJson(feeJson, GoodsInfo.class);
            bindToView();
        }
    }

    private void bindToView() {
        if (goodInfo == null)
            return;
        edit1.setText(goodInfo.getBillNo() + "");
        edit2.setText(goodInfo.getGoodName() + "");
        edit3.setText(goodInfo.getPiece() + "");
        edit4.setText(goodInfo.getWeight() + "");
        edit5.setText(goodInfo.getVolume() + "");
        buttonSend.setText("变更保存");
    }

    @OnClick(R.id.button_send)
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.button_send:

                String billNo=edit1.getText().toString();
                String goodsName=edit2.getText().toString();
                String price=edit3.getText().toString();
                String weight=edit4.getText().toString();
                String volue=edit5.getText().toString();
                if (StringUtil.isEmpty(billNo)){
                    showMsg("请输入提单号");
                    return;
                }
                if (StringUtil.isEmpty(goodsName)){
                    showMsg("请输入货名");
                    return;
                }
                if (StringUtil.isEmpty(price)){
                    showMsg("请输入件箱数");
                    return;
                }
                if (StringUtil.isEmpty(weight)){
                    showMsg("请输入毛重");
                    return;
                }
                if (StringUtil.isEmpty(volue)){
                    showMsg("请输入体积");
                    return;
                }
                if (goodInfo==null){
                    goodInfo=new GoodsInfo();
                    if (dispatchId<1){
                        showMsg("dispatchId错误");
                        return;
                    }
                    goodInfo.setDispatchId(dispatchId);
                }

                goodInfo.setBillNo(billNo);
                goodInfo.setGoodName(goodsName);
                goodInfo.setPiece(Integer.parseInt(price));
                goodInfo.setWeight(Integer.parseInt(weight));
                goodInfo.setVolume(Integer.parseInt(volue));
                List<GoodsInfo> goodsInfoList=new ArrayList<>();
                goodsInfoList.add(goodInfo);
                    GoodsRes goodsRes=new GoodsRes();
                    goodsRes.setToken(token);
                    goodsRes.setData(goodsInfoList);
                    presenter.updateGoodsList(goodsRes);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void check(BaseResponse baseResponse) {
       // BaseResponse baseResponse=gson.fromJson(json,BaseResponse.class);
        if (baseResponse!=null&&baseResponse.getCode()==200){
            showMsg("保存成功");
            setResult(0x512);
            finish();
        }else {
            showMsg(baseResponse.getError());
        }

    }
}
